package com.mobileares.midp.widgets.client.menu;

import com.google.gwt.user.client.Command;
import com.google.gwt.user.client.ui.DecoratedPopupPanel;
import com.google.gwt.user.client.ui.PopupPanel;
import com.google.gwt.user.client.ui.Widget;

/**
 * Created by IntelliJ IDEA.
 * User: Tom
 * Date: 2011-11-14
 * Time: 15:20:25
 * To change this template use File | Settings | File Templates.
 */
public class PopMenu {
    private PopupPanel pop;
    private GWTMenu menu;
    private Widget relative;

    public PopMenu(Widget relative) {
        pop = new DecoratedPopupPanel(true, false);
        pop.setStyleName("myMenuBarPopup");

        menu = new GWTMenu(true);
        pop.setWidget(menu);
        this.relative = relative;
    }

    public PopMenu() {
        this(null);
    }

    public void addMenuItem(GWTMenuItem item) {
        menu.addItem(item);
    }

    public void addMenuItem(String text, Command command) {
        GWTMenuItem item = new GWTMenuItem(text, command);
        menu.addItem(item);
    }

    public void addMenuItem(String text, GWTMenu menu) {
        GWTMenuItem item = new GWTMenuItem(text, menu);
        menu.addItem(item);
    }

    public void showRelativeTo(int x, int y) {
        setPosition(relative.getAbsoluteLeft() + x, relative.getAbsoluteTop() + y);
        pop.show();
    }

    public void show(int left, int top) {
        setPosition(left, top);
        pop.show();
    }

    public void setPosition(int left, int top) {
        pop.setPopupPosition(left, top);
    }

}
